package com.isoft.mapper;

import com.isoft.entity.DfmeCkieEntity;
import org.apache.ibatis.annotations.*;

@Mapper
public interface DfmeCkieMapper {
    @Insert("INSERT INTO MSG_META(SENDER, RECEIVER, SEQUENCE_NUMBER, DATE_TIME, MSG_TYPE, SUB_TYPE) " +
            "VALUES(" +
            "#{meta.sender,jdbcType=VARCHAR}, " +
            "#{meta.receiver,jdbcType=VARCHAR}, " +
            "#{meta.sequenceNumber,jdbcType=NUMERIC}, " +
            "TO_TIMESTAMP(#{meta.dateTime,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), " +
            "#{meta.type,jdbcType=VARCHAR}, " +
            "#{meta.subType,jdbcType=VARCHAR})")
    @Options(useGeneratedKeys = true, keyProperty = "meta.msgId")
    void insertMeta(DfmeCkieEntity entity);

    @Insert({
            "MERGE INTO FLIGHT_BASE fb ",
            "USING (SELECT #{dflt.flightId} AS FLIGHT_ID FROM DUAL) src ",
            "ON (fb.FLIGHT_ID = src.FLIGHT_ID) ",
            "WHEN MATCHED THEN ",
            "UPDATE SET ",
            "fb.FLIGHT_ATTRIBUTE = #{dflt.flightAttribute,jdbcType=VARCHAR}, ",
            "fb.FLIGHT_FILE_ID = #{dflt.flightFileId,jdbcType=VARCHAR}, ",
            "fb.FLIGHT_IDENTIFIER = #{dflt.flightIdentifier,jdbcType=VARCHAR}, ",
            "fb.STATUS = CASE WHEN #{dflt.status,jdbcType=VARCHAR} = 'CKI' THEN 'BOARDING' ELSE COALESCE(NULLIF(#{dflt.status,jdbcType=VARCHAR},''), 'NORMAL') END, ",
            "fb.INTERNAL_STATUS = #{dflt.internalStatus,jdbcType=VARCHAR}, ",
            "fb.MANUAL_STATUS = #{dflt.manualStatus,jdbcType=VARCHAR}, ",
            "fb.MANUAL_INTERNAL_STATUS = #{dflt.manualInternalStatus,jdbcType=VARCHAR} ",
            "WHEN NOT MATCHED THEN ",
            "INSERT (FLIGHT_ID, FLIGHT_ATTRIBUTE, FLIGHT_FILE_ID, FLIGHT_IDENTIFIER, STATUS, INTERNAL_STATUS, MANUAL_STATUS, MANUAL_INTERNAL_STATUS) ",
            "VALUES (",
            "#{dflt.flightId,jdbcType=NUMERIC}, ",
            "#{dflt.flightAttribute,jdbcType=VARCHAR}, ",
            "#{dflt.flightFileId,jdbcType=VARCHAR}, ",
            "#{dflt.flightIdentifier,jdbcType=VARCHAR}, ",
            "CASE WHEN #{dflt.status,jdbcType=VARCHAR} = 'CKI' THEN 'BOARDING' ELSE COALESCE(NULLIF(#{dflt.status,jdbcType=VARCHAR},''), 'NORMAL') END, ",
            "#{dflt.internalStatus,jdbcType=VARCHAR}, ",
            "#{dflt.manualStatus,jdbcType=VARCHAR}, ",
            "#{dflt.manualInternalStatus,jdbcType=VARCHAR})"
    })
    int insertFlightBase(DfmeCkieEntity entity);

    @Insert({
            "<script>",
            "INSERT INTO CHECKIN_COUNTER(COUNTER_ID, FLIGHT_ID, COUNTER_NUMBER, START_TIME, END_TIME, COUNTER_TYPE) ",
            "VALUES ",
            "<if test='dflt.ckls.fces != null'>",
            "(SEQ_COUNTER.nextval, #{dflt.flightId}, #{dflt.ckls.domesticCheckinDesk}, ",
            "TO_TIMESTAMP(#{dflt.ckls.domesticCheckinStart,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), ",
            "TO_TIMESTAMP(#{dflt.ckls.domesticCheckinEnd,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), 'F')",
            "</if>",
            "<if test='dflt.ckls.mces != null'>",
            ", (SEQ_COUNTER.nextval, #{dflt.flightId}, #{dflt.ckls.internationalCheckinDesk}, ",
            "TO_TIMESTAMP(#{dflt.ckls.internationalCheckinStart,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), ",
            "TO_TIMESTAMP(#{dflt.ckls.internationalCheckinEnd,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), 'M')",
            "</if>",
            "</script>"
    })
    int insertCheckinCounter(DfmeCkieEntity entity);
}
